package com.igeekhome.controller.api;

import com.igeekhome.dto.WaybillDto;
import com.igeekhome.pojo.DNetwork;
import com.igeekhome.pojo.WWaybill;
import com.igeekhome.service.IFileUploadService;
import com.igeekhome.service.IOrderNumberListService;
import com.igeekhome.service.IQuoteDtoService;
import com.igeekhome.service.IWaybillService;
import com.igeekhome.util.CommonUtil;
import com.igeekhome.util.ResultBean;
import com.igeekhome.util.TableResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;

/**
 * @author Romeo
 */
@RestController
@RequestMapping("/api/waybill")
public class WaybillControllerApi {

    @Autowired
    private IWaybillService waybillService;

    @Autowired
    private IQuoteDtoService quoteDtoService;

    @Autowired
    private IFileUploadService fileUploadService;

    @Autowired
    private IOrderNumberListService orderNumberListService;

    @RequestMapping("/add")
    public ResultBean add(HttpSession session,WWaybill wWaybill,String sendProvince,String sendCity,String sendArea,String recipientProvince,String recipientCity,String recipientArea){
        DNetwork network = (DNetwork)session.getAttribute("network");
        wWaybill.setNetworkId(network.getNetworkId());
        return waybillService.add(wWaybill,sendProvince,sendCity,sendArea,recipientProvince,recipientCity,recipientArea);
    }

    @RequestMapping("/quote")
    public ResultBean quote(Integer send,Integer destination,Integer shift, Integer itemType,Integer vip){
        
        List<String[]> formula = quoteDtoService.selectFormula(send, destination, shift, itemType, vip);
        if (formula != null) {
            return ResultBean.ok(formula);
        }

        return ResultBean.fail("暂无此报价维护");
    }

    @RequestMapping("/fileUpload")
    public ResultBean upload(@RequestParam("file") MultipartFile file,Integer waybillId) {
        if(waybillId != null) {
            String originalFilename = file.getOriginalFilename();
            FileInputStream inputStream = null;
            try {
                inputStream = (FileInputStream) file.getInputStream();
            } catch (IOException e) {
                e.printStackTrace();
            }
            String uniqueFileName = CommonUtil.getUniqueFileName(originalFilename);
            String src = fileUploadService.uploadFile(inputStream,uniqueFileName);
            return  waybillService.addUrlUpLoad(waybillId, src, originalFilename);
        } else {
            return ResultBean.fail("运单编号未录入");
        }
    }
    @RequestMapping("/increase")
    public ResultBean increase(HttpSession session) {
        DNetwork network = (DNetwork)session.getAttribute("network");
        Integer nid = network.getNetworkId();
        return waybillService.insert(nid);
    }
    
    @RequestMapping("/list")
    public TableResultBean list(HttpSession session,@RequestParam(defaultValue = "1")Integer page, @RequestParam(defaultValue = "10")Integer limit, WaybillDto waybillDto) {
        //获取当前session中网点编号
        DNetwork network = (DNetwork)session.getAttribute("network");
        
        //将网点编号设置为条件查询network(权限)
        waybillDto.setNetworkId(network.getNetworkId());
        TableResultBean tableResultBean = waybillService.waybillList(page, limit, waybillDto);
        System.out.println("---------"+waybillDto+"---------------");
        return tableResultBean;
    }
    
    @RequestMapping("/delete")
    public ResultBean delete(Integer id){
        ResultBean resultBean = waybillService.delete(id);
        return resultBean;
    }
    
    /**
    * @Author 汪璇
    * @Description 更新运单信息
    * @Param [wWaybill, sendProvince, sendCity, sendArea, address, recipientProvince, recipientCity, recipientArea, recipientAddress]
    * @Return com.igeekhome.util.ResultBean
    **/
    @RequestMapping("/update")
    public ResultBean update(HttpSession session,WWaybill wWaybill,String sendProvince,String sendCity,String sendArea,String address,String recipientProvince,String recipientCity,String recipientArea,String recipientAddress){
        DNetwork dNetwork = (DNetwork)session.getAttribute("network");
        ResultBean resultBean = waybillService.update(dNetwork.getNetworkId(),wWaybill,sendProvince,sendCity,sendArea,address,recipientProvince,recipientCity,recipientArea,recipientAddress);
        return resultBean;
    }
    
    
}
